import Vue from "vue";
import VueRouter from "vue-router";
import vi from "element-ui/src/locale/lang/vi";
import store from "@/store";
import tree from "element-ui/packages/table/src/store/tree";
import * as stream from "stream";
import fa from "element-ui/src/locale/lang/fa";
import { auth } from "@/apis";
Vue.use(VueRouter);
import { Message } from "element-ui";
const staticGeneral = [
  {
    path: "/home",
    name: "Home",
    component: () => import("@/views/Home"),
    meta: {
      title: "主页",
    },
  },
  {
    path: "/about",
    name: "About",
    component: () => import("@/views/About.vue"),
    meta: {
      title: "项目成员",
    },
  },
  {
    path: "/test",
    name: "Test",
    component: () => import("@/views/Test.vue"),
    meta: {
      title: "主题颜色",
    },
  },
];

// const account = [
//   {
//     path: "/manageUser",
//     name: "ManageUser",
//     component: () => import("@/views/account/ManageUser"),
//     meta: {
//       title: "用户管理",
//     },
//   },
// ];

// const hr = [
//   {
//     path: "/leaveApply",
//     name: "LeaveApply",
//     component: () => import("@/views/hr/LeaveApply"),
//     meta: {
//       title: "请假申请",
//     },
//   },
//   {
//     path: "/leaveAudit",
//     name: "LeaveAudit",
//     component: () => import("@/views/hr/LeaveAudit"),
//     meta: {
//       title: "请假审核",
//     },
//   },
// ];
const staticPractise = [
  {
    path: "/first",
    name: "First",
    component: () => import("@/views/practise/First.vue"),
    meta: {
      title: "第一练习册",
    },
  },
  {
    path: "/second",
    name: "Second",
    component: () => import("@/views/practise/Second.vue"),
    meta: {
      title: "第二练习册",
    },
  },
];
const staticViews = [...staticPractise, ...staticGeneral];
const content = {
  path: "/content",
  name: "Content",
  component: () => import("@/pages/Content"),
  children: staticViews,
};

const routes = [
  {
    path: "/",
    name: "index",
    // redirect: "/logon",
    component: () => import("@/pages/DevLogon"),
  },
  {
    path: "/logon",
    name: "Logon",
    component: () => import("@/pages/Logon"),
  },
  content,
];

const router = new VueRouter({
  routes,
});

//白名单
//用户是否不登录就能跳转访问页面的白名单
const authWhiteList = ["/", "/logon", "/login"];
//不需要appInit函数预先加载的访问页面
const initWhiteList = ["/", "/logon", "/login"];

router.beforeEach((to, from, next) => {
  /**
   * 正式上线时开启的代码 ，开发模式不建议使用，会导致匿名用户 使用不便。
   * */
  /*  let authpass = false;
  if (authWhiteList.includes(to.path)) {
    authpass = true;
  } else {
    if (store.state.auth) {
      authpass = true;
    } else {
      authpass = false;
    }
  }
  if (!authpass) {
    Message.error("当前用户未登录建议重新登录。");
    next("/logon");
  }*/

  //兼容浏览器的刷新按钮 刷新页面 不至于需要用户重新登录
  if (!initWhiteList.includes(to.path)) {
    if (!store.state.dynamicRoutesAdd) {
      //按照角色加载
      console.warn("用户点击了浏览器刷新按钮，非建议操作");
      if (store.state.auth) {
        store.dispatch("appInit");
      } else {
        store.dispatch("appInit", true);
      }
      store.commit("sureDynamicRoutesAdd");
    }
  }

  next();
});
const breadWhiteList = ["/", "/logon", "/login"];
//路由跳转后渲染页面面包屑
router.afterEach((to, from) => {
  if (!breadWhiteList.includes(to.path)) {
    let rfb = store.state.routesForBread;
    //  加载配置面包屑
    let t = rfb.find((x) => x.path === to.path);
    if (t) {
      console.log("找到面包屑以及其父信息" + t.pids);
      // t.pids="3,6"
      let parentRoutesIds = t.pids.split(",");
      // console.log("总体面包屑信息 "+JSON.stringify(rfb))

      //标准的bread专用格式
      let st = rfb
        .filter((x) => parentRoutesIds.includes(String(x.routeId)))
        .map((x) => {
          return {
            title: x.title,
            path: x.path,
            leave: x.level,
            type: x.type,
          };
        });
      // let st = parentRoutesIds.map((id) => {
      //   let yy = store.state.routesForBread.find((y) => y.routeId === id);
      //   return {
      //     title: y.title,
      //     path: yy.path,
      //     leave: yy.level,
      //     type: yy.type,
      //   };
      // });
      console.log("面包屑标准数据格式：" + JSON.stringify(st));
      st.push({
        title: t.title,
        path: t.path,
        leave: t.level,
        type: t.type,
      });
      st.sort((a, b) => {
        return a.level - b.level;
      });
      store.commit("updateCurrentBreadList", st);
    }
  }
});

export default router;
export { staticGeneral, staticPractise, staticViews, content };
